{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import torch\n",
    "from torchtext.vocab import vocab\n",
    "from collections import Counter, OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle('/home/yx/肺部并发症预测/Data/model_data.pkl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_vocab(df):\n",
    "    all_words = []\n",
    "    for i in df.words:\n",
    "        all_words = np.append(all_words, i)\n",
    "\n",
    "    unique, counts = np.unique(all_words, return_counts=True)\n",
    "\n",
    "    vocabs = []\n",
    "\n",
    "    unique = unique[1100:]\n",
    "    for i in unique:\n",
    "        if len(i) > 2:\n",
    "            vocabs.append(i)\n",
    "    tokens = vocabs\n",
    "    v = vocab(OrderedDict([(token, 1) for token in tokens]))\n",
    "    unk_token = '<unk>'\n",
    "    if unk_token not in v: v.insert_token(unk_token, 0)\n",
    "    v.set_default_index(v[unk_token])\n",
    "    return v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v = get_vocab(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "v.__len__()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.13 ('P_prediction': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "676c8c2553a8289d770cd55f8a54cd76a7646969c2ee13a0fd2fdbb12048cb4a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
